package com.xt.wikiperfumapi.Controller.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.xt.wikiperfumapi.model.IngredientInfo;
import com.xt.wikiperfumapi.model.OrderInfo;
import com.xt.wikiperfumapi.model.OrderPerfumeRef;
import com.xt.wikiperfumapi.model.other.OrderReceipt;
import com.xt.wikiperfumapi.model.other.OrderReceiptInfo;
import com.xt.wikiperfumapi.model.vo.OrderPerfumeRefVo;
import com.xt.wikiperfumapi.model.vo.OrderVo;
import com.xt.wikiperfumapi.model.vo.RestBean;
import com.xt.wikiperfumapi.model.wrapper.OrderWrapper;
import com.xt.wikiperfumapi.service.impl.OrderInfoServiceImpl;
import com.xt.wikiperfumapi.service.impl.OrderPerfumeRefServiceImpl;
import com.xt.wikiperfumapi.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RequestMapping("/api/order")
@RestController
public class OrderController {

    @Resource
    OrderWrapper orderWrapper;

    @Resource
    JwtUtil jwtUtil;

    @Resource
    OrderInfoServiceImpl orderInfoService;
    @Autowired
    private OrderPerfumeRefServiceImpl orderPerfumeRefServiceImpl;

    @RequestMapping("/getAllByMy")
    public RestBean getMyOrder(HttpServletRequest request) {
        String uuid = jwtUtil.getJwtUUID(request);
        List<OrderInfo> orderInfos = orderInfoService.list(new QueryWrapper<OrderInfo>().eq("user_id", uuid).eq("is_deleted", 0));
        List<OrderVo> orderVos = new ArrayList<>();
        orderInfos.forEach(el -> {
            OrderVo orderVo = orderWrapper.orderToVo(el);
            orderVos.add(orderVo);
        });
        return RestBean.success(orderVos);
    }

    @RequestMapping("/create")
    public RestBean addNewOrder(HttpServletRequest request,
                                @RequestBody OrderReceiptInfo orderReceiptInfo) {
        try {
            String uuid = jwtUtil.getJwtUUID(request);
            OrderInfo orderInfo = new OrderInfo();
            String uuidStr = UUID.randomUUID().toString();
            orderInfo.setOrderId(uuidStr);
            orderInfo.setUserId(uuid);
            orderInfo.setAddress(orderReceiptInfo.getAddress());
            orderInfo.setInputTime(new Date());
            orderInfoService.save(orderInfo);

            orderReceiptInfo.getOrderReceipts().forEach(el->{
                OrderPerfumeRef orderPerfumeRef=new OrderPerfumeRefVo();
                orderPerfumeRef.setCount(el.getCount());
                orderPerfumeRef.setPerfumeId(el.getId());
                orderPerfumeRef.setOrderId(uuidStr);
                orderPerfumeRef.setInputTime(new Date());
                orderPerfumeRefServiceImpl.save(orderPerfumeRef);
            });

            return RestBean.success(orderReceiptInfo);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return RestBean.failure(500,"server error");
        }
    }


    @RequestMapping("/list/{page}/{item}")
    public RestBean list(@PathVariable int page , @PathVariable int item){
        PageMethod.startPage(page,item);
        List<OrderInfo> orderInfos =
                orderInfoService.list(new QueryWrapper<OrderInfo>()
                        .eq("is_deleted", 0));
        List<OrderVo> orderVos = new ArrayList<>();
        orderInfos.forEach(el -> {
            OrderVo orderVo = orderWrapper.orderToVo(el);
            orderVos.add(orderVo);
        });
        return RestBean.success(new PageInfo<>(orderVos));
    }

    @RequestMapping("/update")
    public RestBean update(@RequestBody OrderInfo info){
        try {
            info.setUpdateTime(new Date());
            return orderInfoService.updateById(info)?
                    RestBean.success():RestBean.failure(500,"server error");
        }catch (Exception e){
            return RestBean.failure(400,"不存在该订单信息");
        }
    }

    @RequestMapping("/del/{id}")
    public RestBean del(@PathVariable String  id){
        try {
            return orderInfoService.removeById(id)?
                    RestBean.success():RestBean.failure(500,"server error");
        }catch (Exception e){
            return RestBean.failure(400,"不存在该订单信息");
        }
    }

    @RequestMapping("/getid/{id}")
    public RestBean getByid(@PathVariable long id){
        try {
            return RestBean.success(orderInfoService.getById(id));
        }catch (Exception e){
            return RestBean.failure(400,"不存在该订单信息");
        }
    }


}
